import util.IO;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Remark
 */
public class Remark {
    private int baseNum;                    //作业基础项的个数
    private String baseGrade;               //基础成绩
    private String basePrefix;              //评语中基础项的开头
    private String positivePrefix;          //评语中加分项的开头
    private String negativePrefix;          //评语中减分项的开头
    private List<String> gradelist;         //评分等级表

    public int getBaseNum() {
        return baseNum;
    }

    public void setBaseNum(int baseNum) {
        this.baseNum = baseNum;
    }

    public String getBaseGrade() {
        return baseGrade;
    }

    public void setBaseGrade(String baseGrade) {
        this.baseGrade = baseGrade;
    }

    public String getBasePrefix() {
        return basePrefix;
    }

    public void setBasePrefix(String basePrefix) {
        this.basePrefix = basePrefix;
    }

    public String getPositivePrefix() {
        return positivePrefix;
    }

    public void setPositivePrefix(String positivePrefix) {
        this.positivePrefix = positivePrefix;
    }

    public String getNegativePrefix() {
        return negativePrefix;
    }

    public void setNegativePrefix(String negativePrefix) {
        this.negativePrefix = negativePrefix;
    }

    public List<String> getGradelist() {
        return gradelist;
    }

    public void setGradelist(List<String> gradelist) {
        this.gradelist = gradelist;
    }

    /**
     * 根据评语返回rank等级
     * @author ethan
     * @date 2021/7/10 1:29
     * @param file 评语文件
     * @return java.lang.String rank等级
     */
    private  String getRank(String file){
        List<String> remarks = IO.fileToStringList(file,StandardCharsets.UTF_8);
        int baseNumber = 0;         //此份评语中关于基础项的个数
        int positiveNumber = 0;     //此份评语中关于加分项的个数
        int negativeNumber = 0;     //此份评语中关于减分项的个数
        int baseGradeNum = -1;
        int gradelistSize = gradelist.size();
        for(String remark : remarks){
            if(remark.startsWith(basePrefix)){
                baseNumber++;
            }else if(remark.startsWith(positivePrefix)){
                positiveNumber++;
            }else if(remark.startsWith(negativePrefix)){
                negativeNumber++;
            }
        }
        //如果为空，等级为L
        if(baseNumber==0 && positiveNumber == 0 && negativeNumber == 0){
            return "L";
        }
        //如果未完成基础项，等级为R
        if(baseNumber != baseNum){
            return "R";
        }
        //在gradelist中找到baseGrade获取positiveNumber-negativeNumber的数值
        for(int i = 0; i < gradelistSize; i++){
            if (baseGrade.equals(gradelist.get(i))){
                baseGradeNum = i;
                break;
            }
        }
        //防止因为评语的不合理导致ArrayList溢出报错
        if(baseGradeNum - positiveNumber + negativeNumber < 0 || baseGradeNum - positiveNumber + negativeNumber >=gradelistSize){
            System.out.println("评语\"" + file +  "\n不合理");
            return "R";
        }
        //获取最后成绩
        String resultGrade = gradelist.get(baseGradeNum - positiveNumber + negativeNumber);
        //如果最后成绩为`L`,则定为未完成
        return "L".equals(resultGrade)? "R" : resultGrade;
    }
    /**
     *遍历每个同学的当前所有评语，更新rank.csv
     * @author ethan
     * @date 2021/7/10 0:32
     * @param hwdr 评语存放路径
     */
    public void run(String hwdr) {
        File path = new File(hwdr);

        if(baseGrade.isEmpty() || basePrefix.isEmpty() || positivePrefix.isEmpty() || negativePrefix.isEmpty() || gradelist.isEmpty()){
            return;
        }

        //遍历存放所有同学评语的目录
        for(File member : path.listFiles((pathname) -> pathname.isDirectory() && (Pattern.compile("\\d+-\\D+").matcher(pathname.getName())).matches())){
            String currentPath = hwdr + File.separator + member.getName() + File.separator;
            StringBuilder ranks = new StringBuilder();
            ranks.append("hw,rank\n");//存放每个同学的每次作业排名，包括换行
            for(File homeWork : member.listFiles((file) -> file.isFile() && file.getName().startsWith("hw-") && file.getName().endsWith(".txt"))){
                Pattern pattern = Pattern.compile("(hw-\\d+).txt");
                Matcher matcher = pattern.matcher(homeWork.getName());
                if(!matcher.matches()){
                    continue;
                }
                String rank = getRank(currentPath+homeWork.getName());
                String hwName = matcher.group(1);
                ranks.append(hwName + "," + rank + "\n");
            }
            String rankName =  currentPath + "rank.csv";
            IO.stringToFile(rankName, StandardCharsets.UTF_8, ranks.toString());
        }
    }
}